package data.dao.daoImplementation;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import data.dao.*;
import data.dto.*;
import data.searchdto.*;

public class LeaveDetailDAOImpl implements LeaveDetailDAO {

	@Override
	public LeaveDetailDTO findLeaveDetail(
			LeaveDetailSearchDTO leavedetailsearchdto) throws LeaveDAOException, java.sql.SQLException, NotFoundException, SQLException {
		
		String sql = "SELECT * FROM leavedetail WHERE (id = ? ) "; 
        PreparedStatement stmt = null;
        LeaveDetailDTO value = null; 
        try {
             stmt = util.Database.getInstance().getConnection().prepareStatement(sql);
             stmt.setInt(1, leavedetailsearchdto.getLeaveID()); 

             value = singleQuery(stmt);

        } finally {
            if (stmt != null)
                stmt.close();
        }
        return value;
	}

	@Override
	public ArrayList<LeaveDetailDTO> findAllLeaveDetail()
			throws LeaveDAOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void insertLeaveDetail(LeaveDetailDTO LeaveDetail)
			throws LeaveDAOException, java.sql.SQLException, SQLException {

		String sql = "";
		PreparedStatement stmt = null;

		try {
			sql = "INSERT INTO leavedetail ( id, leaveid, leavedetailtype, "
					+ "createdby, createdon) VALUES (?, ?, ?, ?, ?) ";
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);

			stmt.setInt(1, LeaveDetail.getId());
			stmt.setInt(2, LeaveDetail.getLeaveID());
			stmt.setInt(3, LeaveDetail.getLeaveDetailType());
			stmt.setInt(4, util.CurrentUser.getInstance().getUserId());
			stmt.setTimestamp(5, util.CurrentDate.getCurrentTimeStamp());

			int rowcount = stmt.executeUpdate();
			if (rowcount != 1) {
				// System.out.println("PrimaryKey Error when updating DB!");
				throw new SQLException("PrimaryKey Error when updating DB!");
			}

		} finally {
			if (stmt != null)
				stmt.close();
		}

	}

	@Override
	public void updateLeaveDetail(LeaveDetailDTO LeaveDetail)
			throws LeaveDAOException, java.sql.SQLException, NotFoundException,
			SQLException {

		String sql = "UPDATE leavedetail SET leaveid = ?, leavedetailtype = ?, "
				+ " WHERE (id = ? ) ";
		PreparedStatement stmt = null;

		try {
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);
			stmt.setInt(1, LeaveDetail.getLeaveID());
			stmt.setInt(2, LeaveDetail.getLeaveDetailType());

			stmt.setInt(3, LeaveDetail.getId());

			int rowcount = stmt.executeUpdate();
			if (rowcount == 0) {
				// System.out.println("Object could not be saved! (PrimaryKey not found)");
				throw new NotFoundException(
						"Object could not be saved! (PrimaryKey not found)");
			}
			if (rowcount > 1) {
				// System.out.println("PrimaryKey Error when updating DB! (Many objects were affected!)");
				throw new SQLException(
						"PrimaryKey Error when updating DB! (Many objects were affected!)");
			}
		} finally {
			if (stmt != null)
				stmt.close();
		}

	}

	@Override
	public void deleteLeaveDetail(LeaveDetailDTO LeaveDetail)
			throws LeaveDAOException, java.sql.SQLException, NotFoundException,
			SQLException {

		String sql = "DELETE FROM leavedetail WHERE (id = ? ) ";
		PreparedStatement stmt = null;

		try {
			stmt = util.Database.getInstance().getConnection()
					.prepareStatement(sql);
			stmt.setInt(1, LeaveDetail.getId());

			int rowcount = stmt.executeUpdate();
			if (rowcount == 0) {
				// System.out.println("Object could not be deleted (PrimaryKey not found)");
				throw new NotFoundException(
						"Object could not be deleted! (PrimaryKey not found)");
			}
			if (rowcount > 1) {
				// System.out.println("PrimaryKey Error when updating DB! (Many objects were deleted!)");
				throw new SQLException(
						"PrimaryKey Error when updating DB! (Many objects were deleted!)");
			}
		} finally {
			if (stmt != null)
				stmt.close();
		}

	}
	
	protected LeaveDetailDTO singleQuery(PreparedStatement stmt) 
    throws NotFoundException, SQLException, java.sql.SQLException {

    ResultSet result = null;
    LeaveDetailDTO  valueObject = null;

    try {
        result = stmt.executeQuery();

        if (result.next()) {
        	 valueObject = new LeaveDetailDTO();
             valueObject.setId(result.getInt("id")); 
             valueObject.setLeaveID(result.getInt("leaveid")); 
             valueObject.setLeaveDetailType(result.getShort("leavedetailtype")); 
             valueObject.setCreateBy(result.getInt("createdby")); 
             valueObject.setCreatedOn(result.getTimestamp("createdon")); 

        } else {
              //System.out.println("Leavedetail Object Not Found!");
              throw new NotFoundException("Leavedetail Object Not Found!");
        }
    } finally {
        if (result != null)
            result.close();
        if (stmt != null)
            stmt.close();
    }
    return valueObject;
}

}
